package com.lai.note.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lai.note.R;
import com.lai.note.constant.Constant;
import com.lai.note.dto.ResponseResult;
import com.lai.note.service.WenanService;
import com.lai.note.util.RetrofitRequest;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RandomWenanFragment extends Fragment {
    public RandomWenanFragment(){
        super(R.layout.fragment_random_wenan);
    }

    private LinearLayout ramdom_layout;
    private TextView tv_randomWenan;

    //retrofit对象
    private Retrofit retrofit = null;
    private WenanService wenanService = null;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ramdom_layout = getView().findViewById(R.id.random_layout);
        tv_randomWenan = getView().findViewById(R.id.tv_randomWenan);

        ramdom_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                retrofit = RetrofitRequest.getInstance(getContext());
                wenanService = retrofit.create(WenanService.class);
                Call<ResponseResult> call = wenanService.randomWenan();
                call.enqueue(new Callback<ResponseResult>() {
                    @Override
                    public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                        ResponseResult res = response.body();
                        if(res.getStatus()==200){
                            tv_randomWenan.setText(res.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseResult> call, Throwable t) {
                        t.printStackTrace();
                    }
                });
            }
        });

    }
}
